﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using University.Workshop.DataAccess;

namespace University.Workshop.Business
{
    public class ManagerWaterPurchase
    {
        public static void NewWaterPurchase(Model.WaterPurchase newWaterPurchase)
        {
            newWaterPurchase.DateRegistry = DateTime.Now;
            DataAccessWaterPurchase.CreateWaterPurchase(newWaterPurchase);
        }

        public static Model.Cistern GetCisternByPlate(Model.Cistern mp)
        {
            ICollection<Model.Cistern> Cisterns = DataAccess.DataAccessCistern.SearchCisternByPlate(mp);
            if (Cisterns.Count != 1)
                return null;
            else
                return Cisterns.First();
        }


        public static void VerifyDataLicencePlate(Model.WaterPurchase wp)
        {
            Model.Cistern cistern = new Model.Cistern { LicencePlate = wp.LicencePlate };
            if (wp.LicencePlate == "" || wp.LicencePlate == null)
                throw new Exception("La placa es un dato obligatorio para el registro");
            if (wp.LicencePlate.Count() < 6)
                throw new Exception("La placa ingresada no es válida.");
            if (GetCisternByPlate(cistern) == null)
                throw new Exception("La placa ingresada no existe");

        }

        public static void VerifyDataPurchase(Model.WaterPurchase wp)
        {
            if (wp.Amount == 0)
                throw new Exception("Necesita ingresar una cantidad válida");
            if (wp.Amount > int.Parse(wp.Capacity))
                throw new Exception("La cantidad de la compra no puede ser mayor a la capacidad de la cisterna");

            if (wp.TotalPrice == 0 && !wp.Courtesy)
                throw new Exception("Necesita ingresar un total válido");

            if (wp.DatePurchase == null)
                throw new Exception("Necesita ingresar una fecha de compra");
            if (wp.DatePurchase > wp.DateRegistry)
                throw new Exception("Necesita ingresar una fecha válida, no posterior a la fecha actual o de registro en el sistema");
        }



        public static ICollection<Model.WaterPurchase> SearchWaterPurchaseByParameter(Model.WaterPurchase purchasetoFind, String cortesia)
        {
            ICollection<Model.WaterPurchase> u;
            u = DataAccessWaterPurchase.SearchWaterPurchaseByParameterAll(purchasetoFind, cortesia);
            
            if (u.Count == 0)
            {
                throw new Exception("No se encontraron resultados de la busqueda...");
            }
            return u;
        }


        public static ICollection<Model.WaterPurchase> SearchWaterPurchaseMinusDate(Model.WaterPurchase purchasetoFind, String cortesia)
        {
            ICollection<Model.WaterPurchase> u;
            u = DataAccessWaterPurchase.SearchWaterPurchaseMinusDatePurchase(purchasetoFind, cortesia);
            
            if (u.Count == 0)
            {
                throw new Exception("No se encontraron resultados de la busqueda...");
            }
            return u;
        }



        public static Model.WaterPurchase GetWaterPurchaseByLicencePlate(Model.WaterPurchase waterPurchase)
        {
            var purchases = DataAccessWaterPurchase.GetWaterPurchaseByLicencePlate(waterPurchase);
            if (purchases.Count == 0)
                return null;
            else
                return purchases.First();
        }

        public static void UpdateWaterPurchase(Model.WaterPurchase modifyWaterPurchase)
        {
            //ManagerPerson.UpdatePersonIfExisteElseCreate(modifyWaterPurchase.Person);
            //modifyWaterPurchase.UserId = modifyWaterPurchase.Person.PersonId;
            //newUser.IsActive = true;
            //modifyWaterPurchase.Person = null;
            DataAccessWaterPurchase.UpdateWaterPurchase(modifyWaterPurchase);
        }

        public static Model.WaterPurchase GetWaterPurchaseById(DateTime dateTime)
        {
            ICollection<Model.WaterPurchase> waterExpenseList = DataAccessWaterPurchase.GetWaterPurchaseById(dateTime);
            if (waterExpenseList.Count == 0)
            {
                return new Model.WaterPurchase();
            }
            return waterExpenseList.First();
            
        }


        /*
        




        public static Model.User GetUserByLogin(Model.User waterPurchase)
        {
            var persons = DataAccessUser.GetUserByLogin(waterPurchase);
            if (persons.Count == 0)
                return null;
            else
                return persons.First();
        }

        public static void DisableUser(User UserForDisable)
        {
            Model.User u = GetUserByUserId(UserForDisable);
            DataAccessUser.DisableUser(UserForDisable);
        }

        public static void EnableUser(User UserForEnable)
        {
            Model.User u = GetUserByUserId(UserForEnable);
            DataAccessUser.EnableUser(UserForEnable);
        }
         */
    }
}
